﻿using System;
using Guru.Web.UI;
using Guru.Data;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class UserControl_CheckInDetails : GuruUserControl
{
    /// <summary>
    /// If Status=Booking then InvoiceID=BookingID
    /// </summary>
    protected int InvoiceID
    {
        get
        {
            if (string.IsNullOrEmpty(Page.SecureQueryString["InvoiceID"]))
                return 0;
            return int.Parse(Page.SecureQueryString["InvoiceID"]);
        }
    }

    /// <summary>
    /// Selected Rooms
    /// </summary>
    public List<int> RoomList
    {
        set 
        {
            ViewState["RoomList"] = value;
        }
        get 
        {
            if (ViewState["RoomList"] == null)
                ViewState["RoomList"] = new List<int>();
            return ViewState["RoomList"] as List<int>;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.uxCheckInType.BindComboBox("CheckInType");
            // New booking: check in type = date
            if (Request["mode"] == "2" && this.uxCheckInType.Items.Count > 1)
            {
                this.uxCheckInType.SelectedIndex = 1;
            }
            if (this.InvoiceID <= 0)
            {
                this.uxCheckInDate.SelectedDate = DateTime.Now;
                if (Page.IsSecureQueryString && !string.IsNullOrEmpty(Page.SecureQueryString["roomID"]))
                {
                    this.uxSelectedRoom.Value = Page.SecureQueryString["roomID"];
                    this.RoomList.Add(int.Parse(this.uxSelectedRoom.Value));
                }
                this.BindRooms();
            }          
        }        
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string qString = this.uxSelectedRoom.Value.IsNotEmpty() ? Page.BuildSecureQueryString("rooms=" + this.uxSelectedRoom.Value) : string.Empty;
        this.uxSelectRooms.OnClientClick = string.Format("return parent.ShowPopupModalChild(2, 'SelectRoomsModal.aspx?{0}');", qString);
    }
    protected void uxRoomList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        switch (e.Item.ItemType)
        { 
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                DataRowView dv = e.Item.DataItem as DataRowView;
                UserControl_Room room = e.Item.FindControl("uxRoom") as UserControl_Room;
                room.RoomID = dv["RoomID"].ToString();
                room.RoomName = dv["RoomName"].ToSafeString();
                room.Selected = true;
                break;
        }
      
    }
    protected void uxReloadRoom_Click(object sender, EventArgs e)
    {
        this.RoomList = new List<int>();
        string[] rooms = this.uxSelectedRoom.Value.Split(HSMConstants.Separator);
        foreach (string s in rooms)
        {            
            this.RoomList.Add(int.Parse(s));
        }
        this.BindRooms();
    }
    protected void uxClear_Click(object sender, EventArgs e)
    {
        this.RoomList = new List<int>();
        this.uxSelectedRoom.Value = string.Empty;
        this.BindRooms();
    }

    protected void BindRooms()
    {
        string roomIds = string.Empty;
        foreach (int r in this.RoomList)
        {
            roomIds += r.ToString() + HSMConstants.Separator;
        }
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@RoomList", roomIds.Trim(HSMConstants.Separator), DbType.AnsiString);
        DataTable rooms = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Room_GetInfoByRoomIDs", parameters);
        this.uxRoomList.DataSource = rooms;
        this.uxRoomList.DataBind();
    }

    public int Save(HSMEnums.RoomAction action, int guestID)
    {
        double prepaid, promotion = 0;
        double.TryParse(this.uxPrepaid.Text, out prepaid);
        double.TryParse(this.uxPromotion.Text, out promotion);
        string roomIDs = string.Empty;
        foreach (DataListItem item in this.uxRoomList.Items)
        {
            UserControl_Room room = item.FindControl("uxRoom") as UserControl_Room;
            if (room != null && room.Selected) 
                roomIDs += room.RoomID + HSMConstants.Separator;
        }

        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@GuestID", guestID, DbType.Int32);
        
        parameters.Add("@CheckInType", int.Parse(this.uxCheckInType.SelectedValue), DbType.Int32);
        parameters.Add("@CheckInDate", this.uxCheckInDate.SelectedDate.Value, DbType.DateTime);

        if (this.uxCheckOutDate.SelectedDate != null)
            parameters.Add("@CheckOutDate", this.uxCheckOutDate.SelectedDate.Value, DbType.DateTime);
       
        parameters.Add("@Prepaid", prepaid, DbType.Double);
        parameters.Add("@Promotion", promotion, DbType.Double);
        parameters.Add("@Description", this.uxNotes.Text, DbType.String);
        parameters.Add("@RoomIDs", roomIDs.Trim(HSMConstants.Separator), DbType.String);    
        
        string spa_name = string.Empty;
        switch (action)
        {
            case HSMEnums.RoomAction.Book:
                parameters.Add("@ReturnId", 0, DbType.Int32, true);
                parameters.Add("@Status", HSMEnums.BookingStatus.Active, DbType.Int32);
                spa_name = "spa_HSM_Booking_Insert";
                break;
            case HSMEnums.RoomAction.UpdateBook:
                spa_name = "spa_HSM_Booking_Update";
                parameters.Add("@Status", HSMEnums.BookingStatus.Active, DbType.Int32);
                parameters.Add("@BookingId", this.InvoiceID, DbType.Int32);
                break;
            case HSMEnums.RoomAction.CheckIn: 
                spa_name = "spa_HSM_Invoice_Insert";
                parameters.Add("@ReturnId", 0, DbType.Int32, true);
                parameters.Add("@BookingId", this.InvoiceID, DbType.Int32);
                parameters.Add("@RoomNewStatus", (int)HSMEnums.RoomStatus.SingleCheckedIn, DbType.Int32);
                break;
        }
        HSMCommon.HSMBusiness.UpdateData(spa_name, parameters, out outParameters);
        if (outParameters.Count > 0)
        {
            return int.Parse(outParameters[0].ParameterValue.ToString()); 
        }
        else
        {
            return 0;
        }
    }
    public void CancelBook(int bookingID)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParameters = new GuruParameterCollection();
        parameters = new GuruParameterCollection();
        parameters.Add("@BookingId", bookingID, DbType.Int32);
        parameters.AddUserLoggedParams();
        HSMCommon.HSMBusiness.UpdateData("spa_HSM_Booking_Cancel", parameters, out outParameters);
    }
    public void BindBookingDetails(DataTable dt)
    {
        DataRow dr = dt.Rows[0];
        this.uxCheckInDate.SelectedDate = Convert.ToDateTime(dr["CheckInDate"].ToString());
        this.uxCheckOutDate.SelectedDate = Convert.ToDateTime(dr["CheckOutDate"].ToString());
        if (uxCheckInDate.SelectedDate.Value < DateTime.Now)
            this.uxCheckInDate.SelectedDate = DateTime.Now;

        if (dr["Prepaid"].IsNotNull())
        {
            double val = double.Parse(dr["Prepaid"].ToString());
            if (val > 0) this.uxPrepaid.Value = val;
        }
        if (dr["Promotion"].IsNotNull())
        {
            double val = double.Parse(dr["Promotion"].ToString());
            if (val > 0) this.uxPromotion.Value = val;
        }
        this.uxNotes.Text = dr["Description"].ToSafeString();
        this.uxCheckInType.SelectedValue = dr["CheckInType"].ToSafeString();

        if (dr["RoomsList"].IsNotEmpty())
        {
            this.uxSelectedRoom.Value = dr["RoomsList"].ToString();
            string[] rooms = this.uxSelectedRoom.Value.Split(HSMConstants.Separator);
            foreach (string s in rooms)
            {
                this.RoomList.Add(int.Parse(s));
            }
            this.BindRooms();
        }
    }
}
